package com.mofang.powerdekorhelper.activity.shopactivity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.model.Result;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.persenter.BrandDetailPersenter;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.NetUtiles;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.view.BrandDetailView;
import com.mofang.powerdekorhelper.witget.MyWebViewClient;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.io.File;

import butterknife.BindView;

/**
 * Created by luofei on 2017/7/29 0029.
 */

public class BrandDetailActivity extends MvpActivity<BrandDetailView, BrandDetailPersenter> implements BrandDetailView {

    @BindView(R.id.brand_detail_title)
    TitleBar mTitleBar;

    @BindView(R.id.brand_detail_wv)
    WebView mWebview;

    @BindView(R.id.brand_detail_pro)
    ProgressBar loadBar;

    private int factoryId;

    private String qrImageUrl;

    private File localQrcodeImage;

    private int userId;

    private String regionCode;

    private String libs;

    private int brandId;

    private Dialog sharedDialog = null;

    private LoadPrograss loadPrograss;


    @Override
    public BrandDetailPersenter initPresenter() {
        return new BrandDetailPersenter();
    }

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_brand_detail_layout);
        initTitleBarWithback(mTitleBar, R.string.brand_infomaction_text);
        initTitle();

        loadPrograss = new LoadPrograss(this);
    }

    @Override
    public void initData() {

        brandId = getIntent().getIntExtra("brand_id", 0);

        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setDomStorageEnabled(true);
        mWebview.getSettings().setDefaultTextEncodingName("UTF-8");// 设置编码
        mWebview.getSettings().setLoadWithOverviewMode(true);
        mWebview.getSettings().setUseWideViewPort(true);//将图片调整到适合webview的大小
        mWebview.getSettings().setLoadsImagesAutomatically(true);//支持自动加载图片
        mWebview.setHorizontalScrollBarEnabled(false);//水平不显示
        mWebview.setVerticalScrollBarEnabled(false); //垂直不显示
        mWebview.getSettings().setSavePassword(false);

        mWebview.setWebViewClient(new MyWebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                loadBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                loadBar.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);

                L.e("VrPanoramaActivity", "newProgress-------------->" + newProgress);

                loadBar.setProgress(newProgress);

                if (newProgress == 100) {
                    loadBar.setVisibility(View.GONE);
                }
            }
        });

        userId = SharePerforenceUtils.getInstance(this).getUserID();
        regionCode = SharePerforenceUtils.getInstance(this).getAdCode();
        libs = SharePerforenceUtils.getInstance(this).getLatitude() + "," + SharePerforenceUtils.getInstance(this).getLongitude();

        presenter.getJumpconnnection(38, userId, regionCode, libs, brandId, true);
    }

    @Override
    public void setJumpconnnection(Result result) {

        if (result.getResult() != null) {
            mWebview.loadUrl((String) result.getResult());
        }
    }

    @Override
    public void setQRCode(ResultMessage4 qrCode) {
        if (qrCode.getResult() != null && !qrCode.getResult().equals("")) {

            qrImageUrl = qrCode.getResult();

            if (sharedDialog == null) {

                sharedDialog = DialogUtils.MySharedDialog(this, new ShareDialogListener());
            }
            sharedDialog.show();

            showProgress();

            new NetUtiles().bitmapCorvert(this, qrImageUrl, R.string.share_brand_title, "",null, new NetUtiles.ICorvertImage() {
                @Override
                public void corvertSuccess(File file) {
                    localQrcodeImage = file;
                    hideProgress();
                }

                @Override
                public void covertFail() {

                }
            });
        }
    }

    class ShareDialogListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.share_moments_imv:

                   /* new ShareUtils(BrandDetailActivity.this)
                            .shareLocalImage(BrandDetailActivity.this, SHARE_MEDIA.WEIXIN_CIRCLE, localQrcodeImage, "品牌详情分享");*/
                    break;

                case R.id.share_wechat_imv:

                   /* new ShareUtils(BrandDetailActivity.this)
                            .shareLocalImage(BrandDetailActivity.this, SHARE_MEDIA.WEIXIN, localQrcodeImage, "品牌详情分享");*/

                    break;

                case R.id.share_sina_imv:

                  /*  new ShareUtils(BrandDetailActivity.this)
                            .shareLocalImage(BrandDetailActivity.this, SHARE_MEDIA.SINA, localQrcodeImage, "品牌详情分享");*/

                    break;

                case R.id.share_cancle_tv:
                    sharedDialog.dismiss();
                    break;
            }
            sharedDialog.dismiss();
        }
    }

    private void initTitle() {

        mTitleBar.setDividerColor(getResources().getColor(R.color.grey_deep));

        mTitleBar.setActionTextColor(getResources().getColor(R.color.powerdekor));
        mTitleBar.addAction(new TitleBar.TextAction("分享") {
            @Override
            public void performAction(View view) {

                factoryId = SharePerforenceUtils.getInstance(BrandDetailActivity.this).getFactoryId();
                presenter.getQrCode("1", factoryId, 4, BrandDetailActivity.this);
            }
        });
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
    }

    @Override
    public void toast(CharSequence s) {

    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {
        hideProgress();
    }

    @Override
    public void hideNullLayout() {
        hideProgress();
    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {
        hideProgress();
    }

}
